<?php
class My_Controllers_Admin extends Zend_Controller_Action {
	public $order, $username, $ke_id, $group;
	function init() {
		Zend_Layout::startMvc ( array ('layoutPath' => './application/views' ) );
		$this->_helper->layout ()->setViewSuffix ( 'php' );
		//		$response = $this->getResponse ();
		//		$response->insert ( 'footer', $this->view->render ( 'footer.php' ) );
		//		$response->insert ( 'header', $this->view->render ( 'header.php' ) );
		//		
		

		$auth = Zend_Auth::getInstance ();
		
		if (! $auth->hasIdentity ()) {
			$this->_redirect ( '/admin/Login' );
		} else {
			$authDb = $auth->getIdentity ();
			$this->ke_id = $authDb->ke_id;
			Zend_Debug::dump ( $authDb, '$authDb', false );
			$keshi = new Keshi ( );
			$this->kename = $keshi->find ( $this->ke_id )->current ()->kename;
			Zend_Debug::dump ( $this->kename, '$kename', false );
			$this->view->username = $this->username = $authDb->username;
			$this->group = $authDb->group;
			$this->email = $authDb->email;
			$this->id = $authDb->id;
			switch ($this->group) :
				case "section" :
					$this->view->group = "科管理员";
					break;
				case "member" :
					$this->view->group = "信息员";
					break;
				case "admin" :
					$this->view->group = "全站管理员";
					break;
			endswitch;
			
			Zend_Debug::dump ( $this->group, '$group', false );
			$this->makeMenu ();
		}
	}
	function stripslashes_deep($value) {
		if (get_magic_quotes_gpc ()) {
			$value = is_array ( $value ) ? array_map ( stripslashes_deep, $value ) : stripslashes ( $value );
		}
		return $value;
	}
	function makeMenu() {
		$menu = new Zend_Config_Ini ( '../config/Menu.ini', 'menu', array ('allowModifications' => true ) );
		$menu = $menu->menu->toArray ();
		
		if ($this->group == 'admin') {
			$order = 100;
		} elseif ($this->group == 'section') {
			$order = 10;
		} else {
			$order = 1;
		}
		$this->order = $order;
		
		foreach ( $menu as $k => $v ) {
			foreach ( $v ['baby'] as $kk => $vv ) {
				if (! isset ( $vv ['order'] )) {
					$menu [$k] ['baby'] [$kk] ['order'] = 1;
				}
				if ($vv ['order'] > $order) {
					unset ( $menu [$k] ['baby'] [$kk] );
				}
			}
			if (count ( $menu [$k] ['baby'] ) == 0) {
				unset ( $menu [$k] );
			}
		}
		$url = array ('controller' => 'user', 'action' => 'msg', 'id' => $this->id, 'username' => $this->username );
		$user = new User ( );
		$user = $user->find ( $this->id )->current ();
		$url ['kid'] = $this->ke_id;
		$url ['email'] = $user->email;
		$menu [1] ['baby'] [1] ['url'] = $this->view->url ( $url );
		$this->view->menu = $menu;
	}
}
?>